<template>
	<view>
		<view class="auth-box" @click="doNothing" v-if="!showSuccess">
		  <view class="dialog-title">填写信息</view>
		  <view class="dialog-iptbox" v-if="mode == 1">
		    <view class="dialog-img">
		      <image src="/static/image/my/card-2.png" mode="widthFix"></image>
		    </view>
		    <input type="text" class="ipt-username" v-model="idNum" placeholder="填写身份证号后六位" maxlength="6" />
		  </view>
		  <view class="dialog-iptbox" v-if="mode == 1">
		    <view class="dialog-img">
		      <image src="/static/image/my/card-1.png" mode="widthFix"></image>
		    </view>
		    <input type="number" class="ipt-username" v-model="cardNum" placeholder="填写银行卡后四位" maxlength="4" />
		  </view>
		  <view class="dialog-iptbox" v-if="mode == 2">
		    <view class="dialog-img">
		      <image src="/static/image/my/phone.png" mode="widthFix"></image>
		    </view>
		    <input type="number" class="ipt-username" v-model="phoneNum" placeholder="填写手机号" maxlength="11" />
		  </view>
		  <view class="errorBox" v-if="showErrorFlg">
		    <view class="error-img">
		      <image src="/static/image/my/error.png" mode="widthFix"></image>
		    </view>
		    <view class="error-info">{{errorInfo}}</view>
		  </view>
		  <view class="dialog-btnbox">
		    <view class="btn-cancel" @click="goBack">取消</view>
		    <view class="btn-confirm" @click="authConfirm">确定</view>
		  </view>
		</view>
		
		<view class="success-box" v-if="showSuccess">
		  <image src="/static/image/my/success.png"></image>
		  <view class="success-text">添加成功</view>
		  <view class="btn-confirm" @click="goBack">确定</view>
		</view>
	</view>
</template>

<script>
	let app = getApp()
	import utils from '../../../utils/util.js';
	import { http } from '@/utils/commonRequest.js'; // 局部引入
	
	export default {
		data() {
			return {
				showErrorFlg: false,
				couponId: null,
				idNum: null,
				cardNum: null,
				phoneNum: null,
				errorInfo: null,
				showSuccess: false,
				mode: 1,
			}
		},
		
		onLoad: function (options) {
			var couponId = options.id;
			var mode = options.mode;
			this.couponId = couponId;
			this.mode = mode;
		},
		
		methods: {
			goBack: function () {
				uni.navigateBack({
					delta: 1,
				})
			},
			
			authConfirm: function () {
				let mode = this.mode;
				if (mode != 1 && mode != 2) {
					return;
				}
				if (mode == 1) {
					if (utils.isEmpty(this.idNum)) {
						this.showError("请填写身份证后六位");
						return;
					}
					if (this.idNum.length != 6) {
						this.showError("请正确填写身份证后六位");
						return;
					}
					if (utils.isEmpty(this.cardNum)) {
						this.showError("请填写银行卡后四位");
						return;
					}
					if (this.cardNum.length != 4) {
						this.showError("请正确填写银行卡后四位");
						return;
					}
				}
				if (mode == 2) {
					if (utils.isEmpty(this.phoneNum)) {
						this.showError("请填写手机号");
						return;
					}
					if (this.phoneNum.length != 11) {
						this.showError("请正确填写手机号");
						return;
					}
				}
				uni.showLoading({
					title: '正在添加',
				})
				let that = this;
				let params = {
					idNum: that.idNum,
					cardNum: that.cardNum,
					phone: that.phoneNum,
					couponId: that.couponId
				}
				http.post("/managerCoupon/saveWhiteCustomer", params, {
					header: {
						"content-type": "application/x-www-form-urlencoded"
					}
				}).then(res => {
					res = res.data;
					if (200 == res.code) {
						that.showSuccess = true;
					} else {
						that.showError(res.msg);
					}
					uni.hideLoading({
						success: (res) => {},
					})
				})
			},
			
			showError: function (info) {
				that.showErrorFlg = true;
				that.errorInfo = info;
			},
			
			doNothing: function () {
				return;
			},
		}
	}
</script>

<style>
.auth-box {
  padding: 64rpx 54rpx;
  box-sizing: border-box;
  width: 686rpx;
  background: #FFFFFF;
  box-shadow: 0rpx 12rpx 44rpx 0rpx rgba(182, 178, 199, 0.12);
  border-radius: 16rpx;
  margin: 32rpx auto;
}

.dialog-title {
  text-align: center;
  color: #000;
  font-size: 36rpx;
  font-weight: bold;
  margin-bottom: 30rpx;
}

.dialog-iptbox {
  display: flex;
  position: relative;
  padding-left: 16rpx;
  font-size: 28rpx;
  height: 96rpx;
  background: #cccccc2b;
  border-radius: 8rpx;
  align-items: center;
  margin-bottom: 16rpx;
}

.dialog-img {
  width: 48rpx;
  height: 48rpx;
  margin-right: 24rpx;
}

.dialog-img image {
  width: 100%;
}

.img-more {
  width: 32rpx;
  height: 32rpx;
  position: absolute;
  top: 50%rpx;
  right: 24rpx;
}

.img-more image {
  width: 100%;
}

.img-clean {
  width: 48rpx;
  height: 48rpx;
  position: absolute;
  top: 50%rpx;
  right: 20rpx;
}

.img-clean image {
  width: 100%;
}

.dialog-btnbox {
  padding: 40rpx 0rpx;
  display: flex;
  justify-content: space-between;
}

.btn-cancel,
.btn-confirm {
  width: 260rpx;
  height: 84rpx;
  background-color: #fff;
  color: #666;
  font-size: 34rpx;
  line-height: 84rpx;
  border: 1rpx solid #eaeaea;
  border-radius: 42rpx;
  text-align: center;
}

.btn-confirm {
  background: linear-gradient(140deg, #ffc996 0%, #ed176e 100%);
  color: white;
}

.success-box {
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.success-box > image {
  width: 108rpx;
  height: 108rpx;
  margin-top: 112rpx;
}

.success-text {
  font-size: 40rpx;
  font-weight: 500;
  color: #000000;
  margin-top: 80rpx;
  margin-bottom: 64rpx;
}

.error-img {
  width: 32rpx;
  height: 32rpx;
}

.error-img > image {
  width: 100%;
  height: 100%;
}

.errorBox {
  display: flex;
  align-items: center;
}

.error-info {
  font-size: 26rpx;
  font-weight: 600;
  color: #666666;
  margin-left: 6rpx;
}
</style>
